// sort_score.cpp -- 查看成绩排名
#include "../include/student_score_system.h"
#include <iostream>

void sortScore(const System *sys)
{
    system("cls");
    using std::cin;
    using std::cout;
    using std::endl;
    cout << "===================================================" << endl;
    cout << "*                   查看成绩排名                  *" << endl;
    cout << "===================================================" << endl;
    int n;
    cout << "  [1] 语文成绩" << endl;
    cout << "  [2] 数学成绩" << endl;
    cout << "  [3] 英语成绩" << endl;
    cout << "  [4] 总成绩" << endl;
    cout << "===================================================" << endl;
    cout << "请输入选项以选择排名方式: ";
    cin >> n;
    if (cin.fail()) // 判断输入是否为数字，错误则返回1
    {
        while (cin.fail() || n < 1 || n > 9)
        {
            cin.clear();              // 重置错误输入标记
            while (cin.get() != '\n') // 删除错误输入
                continue;
            cout << "输入错误，请重新输入: ";
            cin >> n;
        }
    }
    else // 在输入为数字的情况下，判断数字是否超过范围
    {
        while (n < 1 || n > 9)
        {
            cin.clear();              // 重置错误输入标记
            while (cin.get() != '\n') // 删除错误输入
                continue;
            cout << "输入错误，请重新输入: ";
            cin >> n;
        }
    }

    switch (n)
    {
    case 1:
    {
        system("cls");
        double t = 0;
        System p_sys;
        p_sys = *sys;
        for (int i = 0; i <= p_sys.num - 1; i++)
        {
            for (int j = 0; j <= p_sys.num - i; j++)
            {
                if (p_sys.stu[j].language_ < p_sys.stu[j + 1].language_)
                {
                    t = p_sys.stu[j].language_;
                    p_sys.stu[j].language_ = p_sys.stu[j + 1].language_;
                    p_sys.stu[j + 1].language_ = t;
                }
            }
        }
        cout << "名 次\t"
             << "姓名\t"
             << "分数\n";
        for (int k = 0; k < p_sys.num; k++)
        {
            cout << "第" << k + 1 << "名\t" << p_sys.stu[k].name_ << '\t' << p_sys.stu[k].language_ << endl;
        }
    }
    break;
    case 2:
    {
        system("cls");
        double t = 0;
        System p_sys;
        p_sys = *sys;
        for (int i = 0; i <= p_sys.num - 1; i++)
        {
            for (int j = 0; j <= p_sys.num - i; j++)
            {
                if (p_sys.stu[j].math_ < p_sys.stu[j + 1].math_)
                {
                    t = p_sys.stu[j].math_;
                    p_sys.stu[j].math_ = p_sys.stu[j + 1].math_;
                    p_sys.stu[j + 1].math_ = t;
                }
            }
        }
        cout << "名 次\t"
             << "姓名\t"
             << "分数\n";
        for (int k = 0; k < p_sys.num; k++)
        {
            cout << "第" << k + 1 << "名\t" << p_sys.stu[k].name_ << '\t' << p_sys.stu[k].math_ << endl;
        }
    }
    break;
    case 3:
    {
        system("cls");
        double t = 0;
        System p_sys;
        p_sys = *sys;
        for (int i = 0; i <= p_sys.num - 1; i++)
        {
            for (int j = 0; j <= p_sys.num - i; j++)
            {
                if (p_sys.stu[j].english_ < p_sys.stu[j + 1].english_)
                {
                    t = p_sys.stu[j].english_;
                    p_sys.stu[j].english_ = p_sys.stu[j + 1].english_;
                    p_sys.stu[j + 1].english_ = t;
                }
            }
        }
        cout << "名 次\t"
             << "姓名\t"
             << "分数\n";
        for (int k = 0; k < p_sys.num; k++)
        {
            cout << "第" << k + 1 << "名\t" << p_sys.stu[k].name_ << '\t' << p_sys.stu[k].math_ << endl;
        }
    }
    break;
    case 4:
    {
        system("cls");
        double t = 0;
        System p_sys;
        p_sys = *sys;
        double all_score[p_sys.num];
        for (int i = 0; i < p_sys.num; i++)
        {
            all_score[i] = p_sys.stu[i].language_ + p_sys.stu[i].math_ + p_sys.stu[i].english_;
        }
        for (int i = 0; i <= p_sys.num - 1; i++)
        {
            for (int j = 0; j <= p_sys.num - i; j++)
            {
                if (all_score[j] < all_score[j + 1])
                {
                    t = all_score[j];
                    all_score[j] = all_score[j + 1];
                    all_score[j + 1] = t;
                }
            }
        }
        cout << "名 次\t"
             << "姓名\t"
             << "分数\n";
        for (int k = 0; k < p_sys.num; k++)
        {
            cout << "第" << k + 1 << "名\t" << p_sys.stu[k].name_ << '\t' << all_score[k] << endl;
        }
    }
    break;
    }
    system("pause");
    system("cls");
    showMenu();
    selectOption();
}